package net.eearley.redpoint.app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import net.eearley.redpoint.app.exception.ATMException;
import net.eearley.redpoint.app.exception.ConsoleException;
import net.eearley.redpoint.app.utils.SimpleLogger;

/**
 * The Class SimpleConsole.
 */
public class SimpleConsole {

	/** The app. */
	private App app;

	/**
	 * Instantiates a new simple console.
	 * 
	 * @param app
	 *            the app
	 */
	public SimpleConsole(App app) {
		this.app = app;
	}

	/**
	 * Gets the input.
	 * 
	 * @return the input
	 * 
	 * @throws ConsoleException
	 *             the console exception
	 */
	private String getInput() throws ConsoleException {
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(System.in));
		String line;
		try {
			line = bufferedReader.readLine();
		} catch (IOException e) {
			throw new ConsoleException(e.getMessage(), e);
		}
		return line;
	}

	/**
	 * Parses the input.
	 * 
	 * @param line
	 *            the line
	 * 
	 * @return the action
	 * 
	 * @throws ConsoleException
	 *             the console exception
	 * @throws ATMException
	 *             the ATM exception
	 */
	public Action parseInput(String line) throws ConsoleException, ATMException {
		String input = line.toLowerCase();
		if (input==null | input.length()<1)
			return Action.MORE;

		char firstchar = ' ';
		char secondchar = ' ';
		char thirdchar = ' ';

		if (input.length() > 0)
			firstchar = input.charAt(0);
		if (input.length() > 1)
			secondchar = input.charAt(1);
		if (input.length() > 2)
			thirdchar = input.charAt(2);

		switch (firstchar) {
		case 'q':
			return Action.QUIT;
		case 'r':
			app.setInventory(app.getConfiguration().getInitialDenoms());
			return Action.RESTOCK;
		case 'w':
			app.getValidator().checkHorse(thirdchar, app.getHorseList());
			app.setWinningHorse(Integer.parseInt(thirdchar + ""));
			SimpleLogger.logMessage(app + "");
			return Action.WINNER;
		default:
			SimpleLogger.logMessage(app + "");
			app.getValidator().checkHorse(firstchar, app.getHorseList());
			String vals[] = input.split(" ");
			String betAmt = vals[1];
			app.getValidator().checkBet(betAmt);
			Horse horse = app.getWinningHorse();
			Horse playerHorse = app.getHorse(Integer.valueOf(vals[0] + ""));
			Integer returnOnBet = app.calculateWinnings(playerHorse, horse,
					Integer.parseInt(betAmt));
			if (returnOnBet > 0) {

				SimpleLogger.logMessage("Payout: " + playerHorse.getName()
						+ ", $" + returnOnBet);
				app.getAtm().payout(app.getInventory(), returnOnBet);
			} else {
				SimpleLogger.logMessage("No Payout: " + playerHorse.getName());
			}
			return Action.BET;
		}

	}

	/**
	 * Prompt.
	 */
	public void prompt() {
		try {
			String val = getInput();
			Action action = parseInput(val);
			if (action.equals(Action.QUIT))
				terminate();
			else
				prompt();
		} catch (ConsoleException e) {
			SimpleLogger.logMessage(e.getMessage());
			prompt();
		} catch (ATMException e) {
			SimpleLogger.logMessage(e.getMessage());
			prompt();
		}
		;
	}

	/**
	 * Terminate.
	 */
	public void terminate() {
		System.exit(0);
	}

}
